import os
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
from functools import reduce

import requests


def foo(l):
    return reduce(lambda a, b: a + b, l, 0)


def add_back(f):
    print('call back', f.result())


def process():
    print('cpu_num:', os.cpu_count())
    executor = ProcessPoolExecutor()
    print('executor', executor, type(executor))
    x = range(10000)
    futures = [executor.submit(foo, l) for l in [x[i:i + 1000] for i in range(0, len(x), 1000)]]
    for f in futures:
        print('result:', f.result())
    [executor.submit(foo, l).add_done_callback(add_back) for l in
     [x[i:i + 1000] for i in range(0, len(x), 1000)]]
    executor.shutdown()


def thread():
    def get(url):
        r = requests.get(url)
        return {'url': url, 'text': r.content}

    def parse(future):
        dic = future.result()  # future对象调用result方法取其值
        f = open('db.text', 'a')
        data = 'url:%s\n' % len(dic['text'])
        f.write(data)
        f.close()

    executor = ThreadPoolExecutor()  # 默认cpu core*5
    url_l = ['http://cn.bing.com/', 'http://www.cnblogs.com/wupeiqi/', 'http://www.cnblogs.com/654321cc/',
             'https://www.cnblogs.com/', 'http://society.people.com.cn/n1/2017/1012/c1008-29581930.html',
             'http://www.xilu.com/news/shaonianxinzangyou5gedong.html', ]
    for url in url_l:
        executor.submit(get, url).add_done_callback(parse)  # 与Pool进程池回调函数接收的是A函数的返回值(对象ApplyResult.get()得到的值)。
    executor.shutdown()  # 这里回调函数parse，接收的参数是submit生成的 Future对象。


if __name__ == '__main__':
    process()
    # thread()
